<?php

namespace Admin\Controller;
use Think\Controller;

class PclassController extends CommonController {
    public function index(){
    	$this->display('index');
    }
    
    public function add(){
    	if(IS_POST){
	        $config = array(
		        'maxSize'       =>  3145728 , //上传的文件大小限制 (0-不做限制)
		        'exts'          =>  array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
		        'savePath'      =>  './Uploads/classpic/', //保存路径
	        	'autoSub'       =>  false,
	        	'rootPath'      =>  './Public/', //保存根路径
		        'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
		    );
		    $upload= new \Think\Upload($config);
	    	$info = $upload->uploadOne($_FILES['pic']);;
	    	if(!$info){
	    		dump($upload->getError());
	    		$this->error('图片上传失败！失败原因:'.$info->error);
	    	}else{
	    		$big_pic='./public/'.$info['savepath'].$info['savename'];
	    		
	    		//压缩图片
	    		$image = new \Think\Image();
	    		$image->open($big_pic);
	    		dump($image->thumb(150, 150)->save('./public/'.$info['savepath'].'small_'.$info['savename']));
	    		$file=new File($info['savepath'].$info['savename']);
	    		
	    		//小的图片地址保存到数据库里面
	    		$_POST['pic']=
	    		$data=M('Pclass');
	    		if($data->create()){
	    			if($data->add()){
	    				$this->success("添加成功！");
	    			}else{
	    				$this->error("添加失败！错误代码：001","",5);
	    			}
	    		}else{
	    			$this->error("添加失败！错误代码：001","",5);
	    		}
	    	} 
    	}else{
    		$this->error('非法请求！~');
    	}
    }
    
    public function getclassinfo(){
    	$data=M('pclass')->field('id,name')->select();
    	$this->ajaxReturn($data);
    }
    
    public function ajaxpage(){
    	$User = M('pclass'); 
		$count = $User->count();
		$Page = new \Think\Ajaxpage($count,2);
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');
		$list = $User->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$show = $Page->show();// 分页显示输出
		$arr=array(
			"list"=>$list,
			"show"=>$show
		);
		$this->ajaxReturn($arr);
    }
}